<!--
  功能：报名列表
  作者：licy
  邮箱：licy@briup.com
  时间：2020年11月30日 14:10:00
  版本：v1.0
  修改记录：
  修改内容：
  修改人员：
  修改时间：
-->
<template>
  <div class='apply_list'>
    <!-- 按钮 -->
    <div class="btns">
      <div style="text-align:center;margin-bottom:.5em">
        <el-form size="small" :inline='true'>
          <el-form-item>
            <el-date-picker style="width:120px" v-model="param.year" value-format="yyyy" type="year" placeholder="毕业年份"> </el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-cascader filterable v-model="param.channelIds" clearable :options="channels" placeholder="请选择渠道" :props="{ checkStrictly: true ,children:'children',label:'name',value:'id', emitPath: false,multiple:true}" ></el-cascader>
          </el-form-item>
          <el-form-item>
            <el-cascader filterable v-model="param.clazzId" clearable :options="directionClazzTree" placeholder="请选择班级"
          :props="{ children:'clazzes',label:'name',value:'id',emitPath: false}" ></el-cascader>
          </el-form-item>
          <el-form-item >
            <el-select v-model="param.status" placeholder="状态" style="width:120px" clearable>
              <el-option value="报名中" label="报名中"></el-option>
              <el-option value="待选方向" label="待选方向"></el-option>
              <el-option value="待分班" label="待分班"></el-option>
              <el-option value="学习中" label="学习中"></el-option>
            </el-select>  
          </el-form-item>
          <el-form-item >
            <el-input placeholder="姓名" v-model="param.realname" ></el-input>
          </el-form-item>
        </el-form>
      </div>
      <div style="text-align:right">
        <el-button type="primary" size="small" icon="el-icon-search" @click="loadApplys">搜索</el-button>
        <el-button type="primary" size="small" @click='toBatchConfirmApplyHandler'>批量确认</el-button>
        <el-button type="primary" size="small" @click='toAddHandler'>录入</el-button>
        <el-button type="primary" size="small" @click="toBathImportHandler">批量导入</el-button>
        <el-button type="primary" size="small" @click="toDownloadHandler">导出</el-button>
        <el-button type="danger" size="small" @click="toBatchDeleteHandler">批量删除</el-button>
      </div>
    </div>
    <!-- 按钮 -->
    <!-- 表格 -->
      <el-table :data="applyData.list" size="small" v-loading="loading"  
        :row-class-name="attentionStudentClass"
        @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" fixed="left" > </el-table-column>
        <el-table-column type="index" :index="1" label="序号" align="center" fixed="left"/>
        <el-table-column prop="realname" label="姓名" width="90" align="center" fixed="left"/>
        <el-table-column prop="status" label="状态" width="80" align="center" fixed="left">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.status ==='报名中'" type="info" size="mini">{{scope.row.status}}</el-tag>
            <el-tag v-else-if="scope.row.status==='待分班'" type="warning" size="mini">{{scope.row.status}}</el-tag>
            <el-tag v-else-if="scope.row.status==='离校'" type="danger" size="mini">{{scope.row.status}}</el-tag>
            <el-tag v-else type="success" size="mini">{{scope.row.status}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="学校" width="150" align="center">
          <template slot-scope="scope">
            <div v-if="scope.row.channel">
              {{scope.row.channel.name}}
            </div>
            <div v-else>
              暂无信息
            </div>
          </template>
        </el-table-column>
         <el-table-column label="学院" width="150" align="center">
          <template slot-scope="scope">
            <div v-if="scope.row.channel">
               {{scope.row.channel.parent?scope.row.channel.parent.name:""}}
            </div>
            <div v-else>
              暂无信息
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="major" label="专业" width="150" align="center"/>
        <el-table-column prop="schoolStuClazz" label="校内班级" width="150" align="center"/>
        <el-table-column prop="schoolStuNo" label="校内学号" width="150" align="center"/>
        <el-table-column prop="ddTel" label="钉钉手机号" width="120" align="center"/>
        <el-table-column prop="stuType" label="报名方式" width="80" align="center"/>
        <el-table-column prop="applyTime" label="报名时间" width="150" align="center">
          <template slot-scope="scope">
            {{scope.row.applyTime | fmtTime}}
          </template>
        </el-table-column>
        <el-table-column prop="idCard" label="身份证号" width="180" align="center"/>
        <el-table-column prop="gender" label="性别" width="60" align="center"/>
        <el-table-column prop="birth" label="出生日期" width="120" align="center">
          <template slot-scope="scope">
            {{scope.row.birth | fmtDate}}
          </template>
        </el-table-column>
        <el-table-column prop="education" label="学历" width="80" align="center"/>
        <el-table-column prop="major" label="专业" width="150" align="center"/>
        <el-table-column prop="graduationTime" label="毕业时间" width="120" align="center">
          <template slot-scope="scope">
            {{scope.row.graduationTime | fmtDate}}
          </template>
        </el-table-column>
        <el-table-column prop="flp" label="外语水平" width="80" align="center"/>
        <el-table-column prop="tel" label="手机号" width="120" align="center"/>
        <el-table-column prop="guardianTel" label="监护人手机号" width="120" align="center"/>
        <el-table-column prop="qq" label="qq号" width="120" align="center"/>
        <el-table-column prop="homeAddress" label="家庭住址" width="350" align="center"/>
        <el-table-column prop="isAttention" label="关注" width="120" align="center">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.isAttention == '关系学生'" type="primary" size="small">{{scope.row.isAttention}}</el-tag>
            <el-tag v-else-if="scope.row.isAttention == '问题学生'" type="warning" size="small">{{scope.row.isAttention}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="comment" label="备注" width="180" align="center"/>
        <el-table-column label="籍贯" prop="nativePlace" width="200"></el-table-column>
        <el-table-column label="政治面貌" prop="politicsStatus" width="100"></el-table-column>
        <el-table-column prop="tuition" label="学费" width="80" align="center"/>
        <el-table-column prop="reserve" label="订座费" width="80" align="center"/>
        <el-table-column label="操作" align="center" width="160" fixed="right">
          <template slot-scope="scope">
            <el-button type="text" :disabled="scope.row.status != '报名中'" size="mini" @click.prevent="toConfirmHandler(scope.row)">确认</el-button>
            <el-button type="text" size="mini" @click.prevent="toEditHandler(scope.row)">修改</el-button>
            <el-button type="text" size="mini" @click.prevent="toDeleteHandler(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    <!-- /表格 -->
    <!-- 分页 -->
    <div class="page">
        <el-pagination background 
          layout="prev, pager, next,sizes" 
          small
          :total="applyData.total" 
          :page-size="applyData.pageSize" 
          :current-page="applyData.page"
          :page-sizes="[10, 15, 20,25,50,100]"
          @size-change="handleSizeChange"
          @current-change="currentChangeHandler">
        </el-pagination>
    </div>
    <!-- /分页 -->
     <!-- 导出配置模态框 -->
    <el-dialog title="导出配置" :visible.sync="visible_download" width="30%" class="customer_modal">
      <div style="text-align:center;padding:20px 0">
        <el-button size="small" @click="downloadHandler('current')">导出当前页</el-button>
        <el-button size="small" type="primary" @click="downloadHandler('all')">全部导出</el-button>
      </div>
    </el-dialog>
    <!-- /模态框 -->
    <!-- 确认报名 -->
    <el-dialog title="确认报名信息" width="50%" :visible.sync="visible_confirm_apply" class="customer_modal">
      <el-form label-width="80px" :model="form_apply" ref="confirm_apply_form" :rules="rules_apply" >
        <el-form-item label="学费" prop="tuition">
          <el-input v-model.number="form_apply.tuition"></el-input>
        </el-form-item>
        <el-form-item label="是否订座">
          <el-radio-group v-model="form_apply.isReserve">
            <el-radio :label="0">无</el-radio>
            <el-radio :label="1">有</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="订座费" v-if="form_apply.isReserve === 1">
          <el-input v-model.number="form_apply.reserve"></el-input>
        </el-form-item>
      </el-form>
       <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="visible_confirm_apply = false">取消</el-button>
        <el-button size="small" type="primary" @click="confirmApplyHandler">确认</el-button>
      </span>
    </el-dialog>
    <!-- /确认报名 -->
    <!-- 抽屉 -->
    <Briupdrawer :drawer-visible="visible" :title="title" width="50%" @on-change-visible="handlerVisibleChange" >
      <div slot="content">
        <el-form ref="apply_form" :model="form" :rules="rules" size="mini" label-width="100px" label-position="center">
          <div class="sub_title">基本信息</div>
          <el-row>
            <el-col :span="12">
              <el-form-item label="姓名" prop="realname">
                <el-input v-model="form.realname"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="性别" prop="gender">
                <el-radio-group v-model="form.gender">
                  <el-radio label="男">男</el-radio>
                  <el-radio label="女">女</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="钉钉手机号" prop="ddTel">
                <el-input v-model="form.ddTel"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="手机号" prop="tel">
                <el-input v-model="form.tel"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="qq" prop="qq">
                <el-input v-model="form.qq"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="身份证号" prop="idCard">
                <el-input v-model="form.idCard"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="生日" prop="birth">
                <el-date-picker v-model="form.birth" value-format="timestamp" type="date" placeholder="选择日期" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="监护人" prop="guardian">
                <el-input v-model="form.guardian"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="联系方式" prop="guardianTel">
                <el-input v-model="form.guardianTel"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          
          <el-row>
            <el-col :span="24">
              <el-form-item label="地址" prop="address">
                <el-input v-model="form.homeAddress"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <div class="sub_title">学费信息</div>
          <el-form-item label="学费" prop="tuition">
            <el-input v-model.number="form.tuition"></el-input>
          </el-form-item>
          <el-form-item label="是否订座">
            <el-radio-group v-model="form.isReserve">
              <el-radio :label="0">无</el-radio>
              <el-radio :label="1">有</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="订座费" v-if="form.isReserve === 1">
            <el-input v-model.number="form.reserve"></el-input>
          </el-form-item>

          <div class="sub_title">渠道信息</div>
          <el-form-item label="学校渠道" prop="channelId">
            <el-cascader filterable style="width:100%" v-model="form.channelId" clearable :options="channels" placeholder="请选择院校渠道"
        :props="{ children:'children',label:'name',value:'id', emitPath: false}" ></el-cascader>
          </el-form-item>

          <el-row>
            <el-col :span="12">
              <el-form-item label="校内班级" prop="schoolStuClazz">
                <el-input v-model="form.schoolStuClazz"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="校内学号" prop="schoolStuNo">
                <el-input v-model="form.schoolStuNo"></el-input>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="12">
               <el-form-item label="毕业时间" prop="graduationTime">
                <el-date-picker v-model="form.graduationTime" value-format="timestamp" type="date" placeholder="选择日期" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
               <el-form-item label="专业" prop="major">
                <el-input v-model="form.major"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="学历" prop="education">
                <el-select v-model="form.education">
                  <el-option label="专科" value="专科"></el-option>
                  <el-option label="本科" value="本科"></el-option>
                  <el-option label="研究生" value="研究生"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="外语水平" prop="flp">
                <el-select v-model="form.flp">
                  <el-option value="无" label="无"></el-option>
                  <el-option value="CET4" label="CET4"></el-option>
                  <el-option value="CET6" label="CET6"></el-option>
                  <el-option value="CET8" label="CET8"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
         
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="visible = false">取 消</el-button>
        <el-button type="primary" size="small" @click="submitHandler">确 定</el-button>
      </span>
    </Briupdrawer>
    <!-- /抽屉 -->
    <!-- 批量导入模态框 -->
    <el-dialog title="批量导入报名" width="60%" :visible.sync="visible_import" class="customer_modal">
      <div>
        <p>1. 下载报名表模板</p>
        <div>
          <el-button type="primary" round size="small" @click="downloadTemplate">点击下载</el-button>
        </div>
        <p>2. 选择渠道</p>
        <el-cascader filterable style="width:100%" size="small" v-model="import_data.channelId" clearable :options="channels" placeholder="请选择院校渠道"
        :props="{ children:'children',label:'name',value:'id', emitPath: false}" ></el-cascader>
        <p>3. 上传花名册</p>
        <div style="text-align:center">
          <el-upload
            ref="upload"
            :auto-upload="false"
            class="upload-demo"
            :on-success="successSubmitHandler"
            drag
            :file-list="fileList"
            :data="import_data"
            :action='baseURL+"/student/upload"'
            multiple>
            <i class="el-icon-upload"></i>
            <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
            <div class="el-upload__tip" slot="tip">
              <div style="color:red;font-size:10px;text-align:left">
                <div>1. 表格内容为文本格式，不要掺杂其他数据格式，比如日期</div>
                <div>2. 毕业时间格式为YYYY-MM-DD: 例如：2022-06-30 </div>
                <div>3. 外语水平填写"无"/"CET-4"/"CET-6" </div>
                <div>4. 学历填写"本科"/"专科" </div>
              </div>
            </div>
          </el-upload>
        </div>
      </div>
       <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="visible_import = false">取消</el-button>
        <el-button size="small" type="primary" @click="importSubmitHandler">确定</el-button>
      </span>
    </el-dialog>
    <!--/ 批量导入模态框 -->
  </div>
</template>

<script>
import {get,post,download} from '@/utils/request'
import _ from 'lodash'
import qs from 'qs'
import { mapGetters } from 'vuex'
export default {
  // 组件名称
  name: 'demo',
  // 组件参数 接收来自父组件的数据
  props: {},
  // 组件状态值
  data () {
   return {
      baseURL:process.env.VUE_APP_BASE_API,
      form: {},
      visible_confirm_apply:false,
      loading:false,
      visible: false,
      visible_import:false,
      visible_download:false,
      title: '录入报名信息',
      param:{
        page:1,
        pageSize:15
      },
      fileList:[],
      students:[],// 批量操作的值
      student:null,
      import_data:{},
      applyData:{list:[]},
      rules: {
        ddTel: [ { required: true, message: '请输入钉钉手机号', trigger: 'blur' } ],
        tel: [ { required: true, message: '请输入手机号', trigger: 'blur' } ],
        realname: [ { required: true, message: '请输入姓名', trigger: 'blur' } ],
        qq: [ { required: true, message: '请输入qq', trigger: 'blur' } ],
        gender: [ { required: true, message: '请选择性别', trigger: 'blur' } ],
        idCard: [ { required: true, message: '请输入身份证号', trigger: 'blur' } ],
        birth: [ { required: true, message: '请输入生日', trigger: 'blur' } ],
        flp: [ { required: true, message: '请选择外语水平', trigger: 'change' } ],
        major: [ { required: true, message: '请输入专业', trigger: 'change' } ],
        channelId:[{ required: true, message: '请选择院校渠道', trigger: 'change' }],
        education:[{ required: true, message: '请选择学历', trigger: 'change' }],
        graduationTime:[{ required: true, message: '请选择毕业时间', trigger: 'change' }]
      },
      rules_apply:{
        tuition: [ { required: true, message: '请输入学费信息', trigger: 'blur' } ],
      },
      form_apply:{

      }
   }
  },
  // 计算属性
  computed: {
    ...mapGetters(['channels','directionClazzTree'])
  },
  // 侦听器
  watch: {},
  // 组件方法
  methods: {
    // 重点关注学生
    attentionStudentClass({row, rowIndex}){
      let flag = '';
      switch(row.isAttention){
        case '关系学生':
          flag = 'success-row'
          break;
        case '问题学生':
          flag = 'warning-row'
          break;
        default:
          flag = ''
      }
      return flag;
    },
    toDownloadHandler(){
      this.visible_download = true;
    },
    toBatchDeleteHandler(){
      if(this.students.length>0) {
        this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let ids = this.students.map(item => item.id)
          let url = "/student/batchDelete"
          post(url,{ids}).then(resp =>{
            this.$message({ message: resp.message, type: 'success' })
            this.loadApplys();
          })
        })
      } else {
        this.$message({ message: '请选择要删除的学生', type: 'error' })
      }
    },
    // 去删除
    toDeleteHandler(row){
      this.students = [row];
      this.toBatchDeleteHandler();
    },
    // 批量确认
    toBatchConfirmApplyHandler(){
      if(this.students.length>0) {
        this.visible_confirm_apply = true;
      } else {
        this.$message({ message: '请选择要确认的学生', type: 'error' })
      }
    },
    // 确认报名事件处理函数
    confirmApplyHandler(row){
      this.$refs['confirm_apply_form'].validate((valid) => {
        if (valid) {
          let ids = this.students.map(item => item.id)
          this.form_apply.ids = ids;
          let url = "/student/batchConfirmApplay"
          post(url,this.form_apply).then(response => {
            this.$message({ message: response.message, type: 'success' })
            this.loadApplys();
            this.visible_confirm_apply = false;
          })
        } else {
          return false
        }
      })
    },
    // 确认报名
    toConfirmHandler(row){
      this.visible_confirm_apply = true;
      this.students = [row];
    },
    handleSelectionChange(val) {
      this.students = val;
    },
    // 下载数据
    downloadHandler(type){
       // 去除空值
      let param = _.clone(this.param)
      for(let key in param){
        if(!param[key]){
          delete param[key]
        }
      }
      switch(type){
        case "current":
          break;
        case "all":
          param.pageSize = 500;
          break;
      }
      download('/student/download',param).then(resp => {
        this.visible_download = false;
      })
    },
    // 确认导入
    importSubmitHandler(){
      this.$refs.upload.submit();
    },
    // 下载模板
    downloadTemplate(){
      download("/student/downloadTemplate")
    },
    // 去批量导入
    toBathImportHandler(){
      this.fileList = [];
      this.visible_import = true;
    },
    // 导入成功
    successSubmitHandler(response){
      if(response.status === 200){
        this.visible_import = false;
        this.$message({ message: response.message, type: 'success' })
      } else {
        this.$message({ message: response.message, type: 'error' })
      }
      this.loadApplys();
    },
    // 提交处理函数
    submitHandler() {
      this.$refs['apply_form'].validate((valid) => {
        if (valid) {
          const url = '/student/saveOrUpdate'
          post(url, this.form)
            .then(response => {
              this.visible = false
              this.$message({ message: response.message, type: 'success' })
              this.loadApplys()
            })
        } else {
          return false
        }
      })
    },
    // 去修改
    toEditHandler(row){
      this.form = _.clone(row);
      this.visible = true;
    },
    // 去录入
    toAddHandler(){
      this.form = {};
      this.visible = true;
    },
    // 加载报名信息
    loadApplys(){
      this.loading = true;
      let url = '/student/pageQuery'
      // 去除空值
      for(let key in this.param){
        if(!this.param[key]){
          delete this.param[key]
        }
      }
   
      get(url+"?"+qs.stringify(this.param,{ arrayFormat: 'repeat' }))
      .then(response => {
        this.applyData = response.data
        this.loading = false;
      })
    },
    
    handleSelectionChange(val) {
      this.students = val;
    },
    //处理visible变化的钩子函数,解决抽屉无法收缩的bug
    handlerVisibleChange(val){
      this.visible = val;
    },
    // 当前页发生改变
    currentChangeHandler(page){
      this.param.page = page;
      this.loadApplys();
    },
     // 处理分页大小变化的函数
    handleSizeChange(pageSize){
      // 当前页为第一页
      this.page = 1;
      // 页面大小为用户选择的页面
      this.param.pageSize = pageSize;
      this.loadApplys();
    }
  },
  // 以下是生命周期钩子
  /**
  * 组件实例创建完成，属性已绑定，但DOM还未生成，$ el属性还不存在
  */
  created () {
    // 加载报名信息
    this.loadApplys();
  },
  /**
  * el 被新创建的 vm.$ el 替换，并挂载到实例上去之后调用该钩子。
  * 如果 root 实例挂载了一个文档内元素，当 mounted 被调用时 vm.$ el 也在文档内。
  */
  mounted () {
  },
  /**
  * 实例销毁之前调用。在这一步，实例仍然完全可用。
  */
  beforeDestroy () {
  },
  /**
  * Vue 实例销毁后调用。调用后，Vue 实例指示的所有东西都会解绑定，
  * 所有的事件监听器会被移除，所有的子实例也会被销毁。
  */
  destroyed () {
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<!--使用了scoped属性之后，父组件的style样式将不会渗透到子组件中，-->
<!--然而子组件的根节点元素会同时被设置了scoped的父css样式和设置了scoped的子css样式影响，-->
<!--这么设计的目的是父组件可以对子组件根元素进行布局。-->
<style lang="scss" scoped>

</style>
